package io.renren.softpark.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunlong.common.utils.PageUtils;
import com.yunlong.common.utils.Query;
import com.yunlong.common.utils.R;
import io.renren.softpark.dao.ColumnDao;
import io.renren.softpark.dao.RotationDao;
import io.renren.softpark.dto.RotationDto;
import io.renren.softpark.dto.VerifyRotationDto;
import io.renren.softpark.entity.ColumnEntity;
import io.renren.softpark.entity.RotationEntity;
import io.renren.softpark.service.RotationService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;


@Service("rotationService")
public class RotationServiceImpl extends ServiceImpl<RotationDao, RotationEntity> implements RotationService {

    @Autowired
    ColumnDao columnDao;
    @Autowired
    RotationDao rotationDao;

    @Override
    public void insertRotation(RotationEntity rotationEntity) {
        List<RotationEntity> rotationEntityList = rotationDao.selectList(new QueryWrapper<RotationEntity>());
        rotationDao.insertData(rotationEntity);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<RotationEntity> page = this.page(
                new Query<RotationEntity>().getPage(params),
                new QueryWrapper<RotationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<VerifyRotationDto> verifyRotation(Map<String, Object> params) {
        IPage<RotationEntity> page = this.page(
                new Query<RotationEntity>().getPage(params),
                new QueryWrapper<RotationEntity>()
        );
        List<ColumnEntity> columnEntities = columnDao.selectList(new QueryWrapper<>());
        List<RotationEntity> records = page.getRecords();
        List<VerifyRotationDto> collect1 = records.stream().map((r) -> {
            VerifyRotationDto verifyRotationDto = new VerifyRotationDto();
            BeanUtils.copyProperties(r, verifyRotationDto);
            for (ColumnEntity c : columnEntities) {
                if (c.getColumnId().equals(r.getColumnId())) {
                    verifyRotationDto.setColumnName(c.getColumnName());
                }
            }
            return verifyRotationDto;
        }).collect(Collectors.toList());
        return collect1;
    }

    @Override
    public List<RotationDto> getData() {
        List<RotationEntity> rotationEntityList = rotationDao.selectList(new QueryWrapper<>());
        List<RotationDto> rotationDtos = new ArrayList<>();
        for (RotationEntity rotationEntity : rotationEntityList) {
            RotationDto rotationDto = new RotationDto();
            rotationDto.setRotationId(rotationEntity.getRotationId());
            rotationDto.setColumnId(rotationEntity.getColumnId());
            rotationDto.setColumnName(columnDao.selectById(rotationEntity.getColumnId()).getColumnName());
            rotationDto.setIsRotation(rotationEntity.getIsRotation());
            rotationDto.setSort(rotationEntity.getSort());
            rotationDto.setRotationAddr(rotationEntity.getRotationAddr());
            rotationDtos.add(rotationDto);
        }
        Collections.sort(rotationDtos, new Comparator<RotationDto>() {
            @Override
            public int compare(RotationDto o1, RotationDto o2) {
                if (o1.getSort()>o2.getSort()){
                    return 1;
                }else if (o1.getSort()<o2.getSort()){
                    return -1;
                }else {
                    return 0;
                }

            }
        });
        return rotationDtos;
    }

    @Override
    public RotationDto getOneData(String columnId) {
        RotationDto rotationDto = new RotationDto();
        try {
            ColumnEntity columnEntity = columnDao.selectOne(new QueryWrapper<ColumnEntity>().eq("COLUMN_ID", columnId));
            rotationDto.setColumnId(columnEntity.getColumnId());
            rotationDto.setColumnName(columnEntity.getColumnName());
            rotationDto.setRotationAddr(columnEntity.getShowPic());
            rotationDto.setIsRotation(0);
        }catch (NullPointerException e){
            rotationDto = null;
        }
        return rotationDto;
    }

    @Override
    public RotationDto getOneData(Integer rotationId) {
        RotationEntity rotationEntity = rotationDao.selectOne(new QueryWrapper<RotationEntity>().eq("ROTATION_ID",rotationId));
        RotationDto rotationDto = new RotationDto();

        rotationDto.setColumnId(rotationEntity.getColumnId());
        ColumnEntity columnEntity = columnDao.selectOne(new QueryWrapper<ColumnEntity>().eq("COLUMN_ID", rotationEntity.getColumnId()));
        rotationDto.setColumnName(columnEntity.getColumnName());
        rotationDto.setRotationAddr(rotationEntity.getRotationAddr());
        rotationDto.setSort(rotationEntity.getSort());
        rotationDto.setIsRotation(rotationEntity.getIsRotation());
        rotationDto.setRotationId(rotationId);
        return rotationDto;
    }

    @Override
    public boolean judgeIsRotation(RotationEntity rotation) {
        List<RotationEntity> rotationEntities = rotationDao.selectList(new QueryWrapper<RotationEntity>());
        for(RotationEntity rotationEntity : rotationEntities){
            Integer isRotation = rotationEntity.getIsRotation();
            Integer sort = rotationEntity.getSort();
            if (rotation.getSort().equals(sort)){
                if (isRotation==1&&rotation.getIsRotation()==1){
                    return true;
                }
            }
        }
        return false;
    }


}